
package com.qczl.pme.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qczl.pme.dao.CameraDao;
import com.qczl.pme.dao.FingerDao;
import com.qczl.pme.dao.MettingDao;
import com.qczl.pme.dao.MettingTypeDao;
import com.qczl.pme.dao.StbDao;
import com.qczl.pme.dao.TopicDao;
import com.qczl.pme.dao.VideoDao;
import com.qczl.pme.entity.PMECamera;
import com.qczl.pme.entity.PMEFinger;
import com.qczl.pme.entity.PMEMetting;
import com.qczl.pme.entity.PMEMettingType;
import com.qczl.pme.entity.PMESTB;
import com.qczl.pme.entity.PMESewise;
import com.qczl.pme.entity.PMETopic;
import com.qczl.pme.entity.PMEVideo;
import com.qczl.pme.service.MettingService;
import com.qczl.pme.util.SewiseUtil;
import com.qczl.pme.vo.MettingVo;
import com.qczl.util.ConfigUtil;
import com.qczl.util.DateUtil;
import com.qczl.util.HttpClientUtil;

/**
 * 会议业务逻辑层实现类
 * 
 * @author SongR
 * @date 2017年9月19日下午3:12:42
 */
@Service
public class MettingServiceImpl implements MettingService {

	@Autowired
	MettingDao dao;

	@Autowired
	MettingTypeDao tdao;

	@Autowired
	VideoDao vdao;

	@Autowired
	StbDao stbdao;

	@Autowired
	CameraDao cameradao;

	@Autowired
	TopicDao topicdao;

	@Autowired
	FingerDao fingerdao;

	/**
	 * 获取会议回看列表
	 */
	@Override
	public List<MettingVo> getMettingV1(String cameraid, int start, int size) throws Exception {
		List<MettingVo> vos = new ArrayList<MettingVo>();
		List<PMEVideo> videos = vdao.findP("from PMEVideo where status=0 and mettingId is not null and camera.id="
				+ cameraid + " order by startTime desc", start, size);
		for (PMEVideo v : videos) {
			MettingVo vo = new MettingVo();
			PMEMetting m = dao.get("from PMEMetting where id=" + v.getMettingId());
			if (null != m) {
				PMECamera camera = cameradao.get("from PMECamera where id=" + m.getCamera().getId());
				String url = "http://" + camera.getSewise().getIp();
				if (camera.getSewise().getProt2() != null && !"".equals(camera.getSewise().getProt2())) {
					url += ":" + camera.getSewise().getProt2();
				}
				vo.setEndtime(DateUtil.dataToStr(v.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
				vo.setStarttime(DateUtil.dataToStr(v.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
				vo.setPlayurl(url + v.getPlayUrl());
				vo.setName(m.getTopic().getName());
				vos.add(vo);
			}
		}
		return vos;
	}

	/**
	 * 
	 * @param cameraid
	 * @return
	 * @throws Exception
	 * @author SongR
	 * @version v1
	 * @date 2017年9月19日下午3:11:56
	 */
	@Override
	public Long countMettingV1(String cameraid) throws Exception {
		return vdao.count(
				"select count(*) from PMEVideo where status=0 and mettingId is not null and camera.id=" + cameraid);
	}

	/**
	 * 添加会议记录
	 * 
	 * @author SongR
	 * @version v1
	 * @date 2017年9月19日
	 */
	@Override
	public Long add(String stbno, String topicid, String[] typeid) throws Exception {
		PMESTB stb = stbdao.get("from PMESTB where no='" + stbno + "' and status=0");
		Long mettingid = null;
		if (null != stb) {
			PMECamera camera = cameradao.get(PMECamera.class, stb.getCameraid());
			PMEMetting metting = new PMEMetting();
			if (null != camera) {
				metting.setCamera(camera);
				metting.setTopic(topicdao.get(PMETopic.class, new Long(topicid)));
				metting.setStatus(2);
				metting.setStarttime(new Date());
				mettingid = (Long) dao.save(metting);
				for (String s : typeid) {
					PMEMettingType mt = new PMEMettingType();
					mt.setMettingId(mettingid);
					mt.setTypeId(new Long(s));
					tdao.save(mt);
				}
				List<PMEFinger> fingerArray = fingerdao.find("from PMEFinger where state=0 and stbno='" + stbno + "'");
				for (PMEFinger finger : fingerArray) {
					HttpClientUtil.doGet("http://" + ConfigUtil.getByKey("config", "finger.url") + "?cmd=GetLogData&ip="
							+ finger.getIp() + "&port=" + finger.getPort() + "&type=metting&mettingid=" + mettingid,
							null);
				}

			}
		}
		return mettingid;
	}

	/**
	 * 实现开始会议
	 */
	@Override
	public String startMettingV1(String stbno, String mettingid) throws Exception {
		PMEMetting metting = dao.get(PMEMetting.class, new Long(mettingid));
		String url = "";
		if (null != metting) {
			PMECamera camera = metting.getCamera();
			PMESewise sewise = camera.getSewise();
			JSONObject j = SewiseUtil.recording(sewise, camera.getStreamId(), "sewise.mettingcallback");
			if (null != j && j.getString("success") == "true") {
				PMEVideo video = new PMEVideo();
				video.setCamera(camera);
				video.setCreatTime(new Date());
				video.setMettingId(mettingid);
				video.setStatus(1);
				video.setTaskId(j.getString("taskid"));
				vdao.save(video);
				video.setStartTime(new Date());
				url += camera.getUrl() + "?cameraid=" + camera.getId() + "&taskid=" + j.getString("taskid");
			} else {

			}
		}
		return url;
	}

}
